﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TestOnlineProject.Common;
using TestOnlineProject.Models.DataAccess;
using TestOnlineProject.Models.Entities;

namespace TestOnlineProject.Models.BusinessLogic
{
    public class FeedbackBLO
    {
        FeedbackDAO feedbackDao = new FeedbackDAO();

        public List<Feedback> ToList(ref List<NoModel> noList, int? page, string keyWord = "", string column = "", string sort = "")
        {
            // Feedback list.
            var feedbacks = feedbackDao.Select();

            // Search by key word.
            if (keyWord.Equals(""))
            {
                feedbacks = feedbacks.OrderByDescending(f => f.FeedbackId).ToList();
            }
            else
            {
                feedbacks = feedbacks.Where(f => f.Title.Contains(keyWord)).OrderByDescending(f => f.FeedbackId).ToList();
            }

            // Sort by sort type and solumn name.
            switch (column + sort)
            {
                case "NoAsc":
                    feedbacks = feedbacks.OrderBy(f => f.FeedbackId).ToList();
                    break;
                case "NoDesc":
                    feedbacks = feedbacks.OrderByDescending(f => f.FeedbackId).ToList();
                    break;
                case "TitleAsc":
                    feedbacks = feedbacks.OrderBy(f => f.Content).ToList();
                    break;
                case "TitleDesc":
                    feedbacks = feedbacks.OrderByDescending(f => f.Content).ToList();
                    break;
                case "CreatedTimeAsc":
                    feedbacks = feedbacks.OrderBy(f => f.CreatedTime).ToList();
                    break;
                case "CreatedTimeDesc":
                    feedbacks = feedbacks.OrderByDescending(f => f.CreatedTime).ToList();
                    break;
                case "IsCheckedAsc":
                    feedbacks = feedbacks.OrderBy(f => f.IsChecked).ToList();
                    break;
                case "IsCheckedDesc":
                    feedbacks = feedbacks.OrderByDescending(f => f.IsChecked).ToList();
                    break;
            }

            // Generate no. List.
            foreach (var v in feedbacks)
            {
                noList.Add(new NoModel() { Id = v.FeedbackId });
            }
            noList = noList.OrderBy(n => n.Id).ToList();
            for (int i = 0; i < noList.Count; i++)
            {
                noList[i].No = i + 1;
            }

                return feedbacks;
        }

        public Feedback Find(int feedbackId)
        {
            try
            {
                var feedback = feedbackDao.SelectByFeedbackId(feedbackId);
                return feedback;
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// Insert new feedback
        /// </summary>
        /// <param name="feedback"></param>
        /// <returns></returns>
        public int Add(Feedback feedback)
        {
            feedback.CreatedTime = DateTime.Now;
            feedback.IsChecked = false;

            // Add user.
            int result = feedbackDao.Insert(feedback);

            return result;
        }

        public int Uncheck(string ids = "")
        {
            var spliter = new StringHelper();

            // Split id string to int array.
            int[] idInt = spliter.Split(ids);

            int result = 0;

            // Deactivate violations.
            foreach (var i in idInt)
            {
                // Select violation by id.
                var feedback = feedbackDao.SelectByFeedbackId(i);

                // Check violation IsChecked?
                //if (feedback.IsChecked)
                //{
                    feedback.IsChecked = false;
                    result += feedbackDao.Update(feedback);
                //}
            }

            return result;
        }

        public int Check(string ids = "")
        {
            var spliter = new StringHelper();

            // Split id string to int array.
            int[] idInt = spliter.Split(ids);

            int result = 0;

            // Deactivate violations.
            foreach (var i in idInt)
            {
                // Select violation by id.
                var feedback = feedbackDao.SelectByFeedbackId(i);

                // Check violation IsChecked?
                //if (!feedback.IsChecked)
                //{
                    feedback.IsChecked = true;
                    result += feedbackDao.Update(feedback);
                //}
            }

            return result;
        }

        public int Remove(string ids = "")
        {
            try
            {
                StringHelper spliter = new StringHelper();

                // Split id string to int array.
                int[] idInt = spliter.Split(ids);

                int result = 0;

                // Delete subjects.
                foreach (var i in idInt)
                {
                    // Check subject is deactivated?
                    result += feedbackDao.Delete(i);
                }

                return result;
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}